#include <iostream>
using namespace std;

struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode* left, TreeNode* right)
        : val(x), left(left), right(right) {}
};

TreeNode* merge(TreeNode* t1, TreeNode* t2) {
    if (t1 == nullptr) return t2;
    if (t2 == nullptr) return t1;
    TreeNode* t = new TreeNode(t1->val + t2->val, merge(t1->left, t2->left),
                               merge(t1->right, t2->right));
    return t;
}

TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
    return merge(root1, root2);
}